package com.itheima.controller;

import com.itheima.common.BatchReqDto;
import com.itheima.common.PageReqDto;
import com.itheima.common.PageResult6;
import com.itheima.entity.Question;
import com.itheima.entity.QuestionsAudit;
import com.itheima.entity.QuestionsRecord;
import com.itheima.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private HttpServletRequest httpServletRequest;
    //审核意见
    @GetMapping("/{id}/auditOpinions")
    public List<QuestionsAudit> auditOpinions(@PathVariable Integer id){
        return questionService.auditOpinions(id);
    }
    //出题记录
    @GetMapping("/{id}/setRecords")
    public List<QuestionsRecord> setRecords(@PathVariable Integer id){
        return  questionService.setRecords(id);
    }
    //基础题库列表
    @GetMapping
    public PageResult6 findAllBaseQues(Integer page,Integer pagesize,String keyword,String shortName,String difficulty,String questionType,String tags,String province,String city,String remarks,String direction,Integer creatorID,Integer catalogID,Integer chkState) {
        HttpSession session = httpServletRequest.getSession();
        String email = (String) session.getAttribute("username");
        PageReqDto pageReqDto = new PageReqDto();
        pageReqDto.setPage(page);
        pageReqDto.setPagesize(pagesize);
        pageReqDto.setKeyword(keyword);
        pageReqDto.setShortName(shortName);
        pageReqDto.setDifficulty(difficulty);
        pageReqDto.setQuestionType(questionType);
        pageReqDto.setTags(tags);
        pageReqDto.setProvince(province);
        pageReqDto.setCity(city);
        pageReqDto.setRemarks(remarks);
        pageReqDto.setDirection(direction);
        pageReqDto.setCreatorId(creatorID);
        pageReqDto.setCatalogID(catalogID);
        pageReqDto.setChkState(chkState);
        pageReqDto.setIsChoice(0);
        return questionService.findAllBaseQues(pageReqDto,email);
    }
    //基础题库删除
    @DeleteMapping("/{id}")
    @Transactional
    public void DeleteQue(@PathVariable Integer id) {
        questionService.deleteById(id);
    }


    //基础题库添加
    @PostMapping
    @Transactional
    public void addQue( @RequestBody  Question question) {
        HttpSession session = httpServletRequest.getSession();
        String email = (String) session.getAttribute("username");
        questionService.addQue(question,email);
    }



    //基础题库详情
    @GetMapping("/{id}")
    public Question findQueById(@PathVariable Integer id) {
        return questionService.findById(id);
    }
    //基础题库修改
    @PutMapping("/{id}")
    @Transactional
    public void modifyById(@PathVariable Integer id,@RequestBody Question question) {
        questionService.modifyById(id,question);
    }

    @PostMapping("/batch")
    //批量导入暂时没学先略过
    public void batch(@RequestBody BatchReqDto batchReqDto) {

    }
    //精选题库上下架
    @PostMapping("/choice/{id}/{publishState}")
    public void changePublishState (@PathVariable Integer id,@PathVariable Integer publishState){
        questionService.changePublishState(id,publishState);
    }
    //精选题库列表
    @GetMapping("/choice")
    public PageResult6 findQualityQues(Integer page,Integer pagesize,String keyword,String shortName,String difficulty,String questionType,String tags,String province,String city,String remarks,String direction,Integer creatorID,Integer catalogID,Integer chkState) throws IOException {
        HttpSession session = httpServletRequest.getSession();
        String email = (String) session.getAttribute("username");
        PageReqDto pageReqDto = new PageReqDto();
        pageReqDto.setPage(page);
        pageReqDto.setPagesize(pagesize);
        pageReqDto.setKeyword(keyword);
        pageReqDto.setShortName(shortName);
        pageReqDto.setDifficulty(difficulty);
        pageReqDto.setQuestionType(questionType);
        pageReqDto.setTags(tags);
        pageReqDto.setProvince(province);
        pageReqDto.setCity(city);
        pageReqDto.setRemarks(remarks);
        pageReqDto.setDirection(direction);
        pageReqDto.setCreatorId(creatorID);
        pageReqDto.setCatalogID(catalogID);
        pageReqDto.setChkState(chkState);
        pageReqDto.setIsChoice(1);
        return questionService.findQualityQues(pageReqDto,email);
    }
    //组题列表 前端模拟生成的
    @GetMapping("/randoms")
    public PageResult6 findAllRandoms(@RequestBody PageReqDto pageReqDto){
        return questionService.findAllRandoms(pageReqDto);
    }
    @DeleteMapping("/randoms/{id}")
    public void deleteRandom(@PathVariable Integer id){
        questionService.deleteRandom(id);
    }
    @PatchMapping("/choice/{id}/{choiceState}")
    public void changeChoiceState(@PathVariable Integer id,@PathVariable Integer choiceState){
        questionService.changeChoiceState(id,choiceState);
    }
    @PostMapping("/check/{id}")
    public void changeCheck(@PathVariable Integer id,@RequestBody Question que){
        questionService.changeCheck(id,que);
    }


}
